﻿var functions = [];
var cfid = null;

$(function() {
    $.ajax({
        type: "get",
        url: "api/access.ashx",
        data: { type: 'getuserfunctions', r: Math.random() },
        dataType: "json",
        success: function(data) {
            functions = data;

            $.each(data, function(i, item) {
                var newul = $("<ul/>");
                var newli = $("<li/>");
                cfid = item.functions[0].fid;
                newli.append("<a href='" + item.functions[0].url + "' target='main' rel='" + item.gid + "' fid='" + cfid + "'>" + item.gname + "</a>");

                var ful = $("<ul/>");

                $.each(item.functions, function(j, fitem) {
                    var fli = $("<li/>");
                    if (j < item.functions.length - 1) {
                        fli.css("border-bottom-style", "none");
                    }
                    fli.append("<a href='" + fitem.url + "' target='main' rel='" + item.gid + "' fid='" + fitem.fid + "'>" + fitem.fname + "</a>");

                    ful.append(fli);
                });
                newli.append(ful);

                newul.append(newli);
                $(".sysmenu").append(newul);

                if (i == 0) {
                    //createMenu(item);
                }
            });

            $(".sysmenu a").click(function() {
                var _this = $(this);
                cfid = _this.attr("fid");
                $.each(functions, function(i, item) {
                    if (_this.attr("rel") == item.gid) {
                        createMenu(item);
                    }
                });
            });
        }
    });
    $(".userInfo a").click(function() {

        $.ajax({
            type: "POST",
            url: "/api/user.ashx",
            data: { type: 'logout' },
            dataType: "string",
            success: function(data) {
                if (data == 1) {
                    window.location.href = "";
                }
                else {
                    facebox("系统错误！", false);
                }
            }
        });

        return false;
    });
});
function createMenu(item) {
    
    $("#dv_Menu").empty();

    $("#dv_Menu").append('<div class="SectionTitle">' + item.gname + '</div>');
    
    $.each(item.functions, function(i, fitem) {
        var currentClass = "";
        if (fitem.fid == cfid) {
            currentClass = " class='current'";
            $(".desc").html("您当前的位置：" + fitem.fname);
        }
        $("#dv_Menu").append('<div class="GroupTitle">\
                            <div><a href="' + fitem.url + '" target="main"' + currentClass + '>' + fitem.fname + '</a></div>\
                        </div>');
        $(".GroupTitle a").click(function() {
            $(".GroupTitle a").removeClass("current");
            $(this).addClass("current");
            $(".desc").html("您当前的位置：" + fitem.fname);
        });
    });
}
function facebox(msg,autoclose) {    
    $.facebox(msg);
    $("#facebox .footer").empty().append('\
        <a href="#" class="close"> \
            <img src="images/closelabel.gif" title="close" class="close_image" /> \
        </a>');
    $("#facebox .close").bind("click", closebox);

    setTimeout("closebox();", 3000);
}
function confirmbox(msg) {
    $.facebox(msg);
    $("#facebox .footer").empty().append(' \
        <input type="button" id="btn_sure" class="btn" value="确定"/>&nbsp; \
        <input type="button" id="btn_cancel" class="btn" value="关闭"/> \
    ');
    $("#btn_cancel").bind("click", closebox);
}
function closebox() {
    $.facebox.close();
    return false;
}

function openDialog(src, title, width, height) {
    //$("#dv_dialog").dialog("destroy");
    
    if ($("#dv_dialog").length == 0) {
        $("<div id=\"dv_dialog\"><iframe id=\"ifr_detail\" name=\"ifr_detail\" src=\"\" width=\"96%\" frameborder=\"0\" ></iframe></div>").appendTo($("body"));
        $("#dv_dialog").dialog({
            autoOpen: false,
            width: width,
            height: height,
            modal: true,
            resizable: false
        });
    }


    $("#dv_dialog").dialog("option", { title: title, width: width, height: height, position: "center" });
    $("#dv_dialog iframe").attr("src", src);    
    $("#dv_dialog").dialog("open");
    
}
function closeDialog() {
    $("#dv_dialog").dialog("close");
}